package com.droidski.taskdb;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util {

	public static String formatDate(long timeInMillis) {
		Date date = Calendar.getInstance().getTime();
		date.setTime(timeInMillis);
		SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
		return sdf.format(date);
	}
	
	public static String formatTimeSpent(long time) {
		
		long ONE_MINUTE = 60 * 1000;
		long ONE_HOUR = 60 * ONE_MINUTE;
		long ONE_DAY = 24 * ONE_HOUR;
		
		int total = (int)time;
		int days = (int)(total / ONE_DAY);
		total-=(days*ONE_DAY);
		
		int hours = (int)(total/ONE_HOUR);
		total-=(hours*ONE_HOUR);
		
		int minutes = (int)(total/ONE_MINUTE);
		total-=(minutes*ONE_MINUTE);
		
		StringBuffer sb = new StringBuffer();
		if ( days > 0 ) {
			sb.append(days);
			sb.append("d");
		}
		if ( hours > 0 ) {
			sb.append(hours);
			sb.append("h");
		}
		if ( minutes > 0 ) {
			sb.append(minutes);
			sb.append("m");
		}
		return sb.toString();
	}
	
	public static String rpad(String padding, int size) {
		return rpad("", padding, size);
	}

	public static String repeat(String str, int times) {
		StringBuffer sb =new StringBuffer();
		for ( int i=0; i<times; i++) {
			sb.append(str);
		}
		return sb.toString();
	}

	public static String rpad(String str, String padding, int size) {
		if ( str.length() < size ) {
			StringBuffer sb = new StringBuffer();
			sb.append(str);
			for ( int i=str.length(); i<size; i++) {
				sb.append(padding);
			}
			return sb.toString();
		} else {
			return str;
		}
	}
	
	public static long getTimeToday() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}
	
	public static long getTimeWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}

	public static long getTimeMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}

}
